import {
    NextFunction,
    Request,
    Response,
} from 'express';

import DataEntity from '../entity/DataEntity';
import BaseService from '../service/BaseService';
import BaseViewContorller from './BaseViewController';

/**
 * 数据查看
 */
export default abstract class BaseEntityContorller<T extends DataEntity> extends BaseViewContorller<T> {
    constructor(service: BaseService<T>) {
        super(service);
    }
    /**
     * 校验数据的合法性
     */
    protected abstract validate(data: T): boolean;
    /**
     * 创建实体实例
     */
    protected abstract newInstance(): T;

    /**
     * 添加数据
     * @param req 请求
     * @param res 返回
     * @param next 
     */
    save(req: Request, res: Response, next: NextFunction) {
        let param = req.body;
        if (!param) {
            res.status(400).send({
                code: 400,
                msg: '参数为空'
            });
            return;
        }
        //做数据的具体校验
        let entity: T = this.newInstance();
        if (!this.validate(Object.assign(entity, param))) {
            res.status(400).send({
                code: 400,
                msg: '参数不合法'
            });
            return;
        }
        //数据权限校验
        try {
            this.service.save(entity);
            res.send({
                code: 200,
                msg: ''
            })
        } catch (error) {
            console.error(error);
            res.status(500).send({
                code: 500,
                msg: '处理失败'
            });
        }
    }
    /**
     * 更新数据
     * @param req 请求
     * @param res 返回
     * @param next 
     */
    update(req: Request, res: Response, next: NextFunction) {
        let param = req.body;
        if (!param) {
            res.status(400).send({
                code: 400,
                msg: '参数为空'
            });
            return;
        }
        //做数据的具体校验
        let entity: T = this.newInstance();
        if (!this.validate(Object.assign(entity, param))) {
            res.status(400).send({
                code: 400,
                msg: '参数不合法'
            });
            return;
        }
        //数据权限校验
        try {
            this.service.update(entity);
            res.send({
                code: 200,
                msg: ''
            })
        } catch (error) {
            console.error(error);
            res.status(500).send({
                code: 500,
                msg: '处理失败'
            });
        }
    }

    /**
     * 删除数据
     * @param req 
     * @param res 
     * @param next 
     */
    deleteData(req: Request, res: Response, next: NextFunction) {
        let id = req.query.id;
        if (!id) {
            res.status(400).send({
                code: 400,
                msg: '参数为空'
            });
            return;
        }
        try {
            this.service.deleteData(parseInt(<string>id));
            res.send({
                code: 200,
                msg: ''
            })
        } catch (error) {
            console.error(error);
            res.status(500).send({
                code: 500,
                msg: '处理失败'
            });
        }
    }
}
